package com.ruoyi.controller.system;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.enums.UserIsAdminType;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.domain.SysUserAccount;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.SysIndexStatisticsRes;
import com.ruoyi.system.vo.req.SysOrderReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.SysUser;

/**
 * 首页 业务处理
 * 
 * @author ruoyi
 */
@Controller
public class SysIndexController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISysOrderService sysOrderService;

    @Autowired
    private ISysWithdrawService sysWithdrawService;

    @Autowired
    private ISysUserService sysUserService;


    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme"));
        mmap.put("skinName", configService.selectConfigByKey("sys.index.skinName"));
        mmap.put("copyrightYear", Global.getCopyrightYear());
        mmap.put("demoEnabled", Global.isDemoEnabled());
        return "index";
    }

    // 切换主题
    @GetMapping("/system/switchSkin")
    public String switchSkin(ModelMap mmap)
    {
        return "skin";
    }

    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        mmap.put("user", user);

        SysIndexStatisticsRes sysIndexStatisticsRes = new SysIndexStatisticsRes();
        Date todayBegin = DateUtil.beginOfDay(new Date());
        Date monthBegin = DateUtil.beginOfMonth(new Date());
        if (user.getUserType() == UserIsAdminType.ADMIN.getCode()) {
            sysIndexStatisticsRes.setTodayOrderAmount(sysOrderService.selectSysOrderAmount(new SysOrderReq(), todayBegin));
            sysIndexStatisticsRes.setMonthOrderAmount(sysOrderService.selectSysOrderAmount(new SysOrderReq(), monthBegin));
            sysIndexStatisticsRes.setAllOrderAmount(sysOrderService.selectSysOrderAmount(new SysOrderReq(), null));
            BigDecimal todayWithdrawAmount = sysWithdrawService.selectSysWithdrawAmount(null,todayBegin);
            BigDecimal monthWithdrawAmount = sysWithdrawService.selectSysWithdrawAmount(null,monthBegin);
            BigDecimal allWithdrawAmount = sysWithdrawService.selectSysWithdrawAmount(null,null);
            sysIndexStatisticsRes.setTodayWithdrawAmount(todayWithdrawAmount);
            sysIndexStatisticsRes.setMonthWithdrawAmount(monthWithdrawAmount);
            sysIndexStatisticsRes.setAllWithdrawAmount(allWithdrawAmount);
        }else {
            if (user.getUserType() == UserIsAdminType.HIGHAGENT.getCode()){
                SysOrderReq sysOrderReq = new SysOrderReq();
                Map map = new HashMap();
                map.put("parentId",user.getUserId());
                sysOrderReq.setParams(map);
                sysIndexStatisticsRes.setTodayOrderAmount(sysOrderService.selectHighAgentSysOrderAmount(sysOrderReq, todayBegin));
                sysIndexStatisticsRes.setMonthOrderAmount(sysOrderService.selectHighAgentSysOrderAmount(sysOrderReq, monthBegin));
                sysIndexStatisticsRes.setAllOrderAmount(sysOrderService.selectHighAgentSysOrderAmount(sysOrderReq, null));
                BigDecimal todayWithdrawAmount = sysWithdrawService.selectHighAgentSysWithdrawAmount(null,todayBegin,user.getUserId());
                BigDecimal monthWithdrawAmount = sysWithdrawService.selectHighAgentSysWithdrawAmount(null,monthBegin,user.getUserId());
                BigDecimal allWithdrawAmount = sysWithdrawService.selectHighAgentSysWithdrawAmount(null,null,user.getUserId());
                sysIndexStatisticsRes.setTodayWithdrawAmount(todayWithdrawAmount);
                sysIndexStatisticsRes.setMonthWithdrawAmount(monthWithdrawAmount);
                sysIndexStatisticsRes.setAllWithdrawAmount(allWithdrawAmount);
            } else if (user.getUserType() == UserIsAdminType.AGENT.getCode()){
                SysOrderReq sysOrderReq = new SysOrderReq();
                Map map = new HashMap();
                map.put("parentId",user.getUserId());
                sysOrderReq.setParams(map);
                sysIndexStatisticsRes.setTodayOrderAmount(sysOrderService.selectAgentSysOrderAmount(sysOrderReq, todayBegin));
                sysIndexStatisticsRes.setMonthOrderAmount(sysOrderService.selectAgentSysOrderAmount(sysOrderReq, monthBegin));
                sysIndexStatisticsRes.setAllOrderAmount(sysOrderService.selectAgentSysOrderAmount(sysOrderReq, null));
                BigDecimal todayWithdrawAmount = sysWithdrawService.selectAgentSysWithdrawAmount(null,todayBegin,user.getUserId());
                BigDecimal monthWithdrawAmount = sysWithdrawService.selectAgentSysWithdrawAmount(null,monthBegin,user.getUserId());
                BigDecimal allWithdrawAmount = sysWithdrawService.selectAgentSysWithdrawAmount(null,null,user.getUserId());
                sysIndexStatisticsRes.setTodayWithdrawAmount(todayWithdrawAmount);
                sysIndexStatisticsRes.setMonthWithdrawAmount(monthWithdrawAmount);
                sysIndexStatisticsRes.setAllWithdrawAmount(allWithdrawAmount);
            }else {
                SysOrderReq sysOrderReq = new SysOrderReq();
                sysOrderReq.setUserCode(user.getUserCode());
                sysIndexStatisticsRes.setTodayOrderAmount(sysOrderService.selectSysOrderAmount(sysOrderReq, todayBegin));
                sysIndexStatisticsRes.setMonthOrderAmount(sysOrderService.selectSysOrderAmount(sysOrderReq, monthBegin));
                sysIndexStatisticsRes.setAllOrderAmount(sysOrderService.selectSysOrderAmount(sysOrderReq, null));
                BigDecimal todayWithdrawAmount = sysWithdrawService.selectSysWithdrawAmount(user.getUserCode(), todayBegin);
                BigDecimal monthWithdrawAmount = sysWithdrawService.selectSysWithdrawAmount(user.getUserCode(), monthBegin);
                BigDecimal allWithdrawAmount = sysWithdrawService.selectSysWithdrawAmount(user.getUserCode(), null);
                sysIndexStatisticsRes.setTodayWithdrawAmount(todayWithdrawAmount);
                sysIndexStatisticsRes.setMonthWithdrawAmount(monthWithdrawAmount);
                sysIndexStatisticsRes.setAllWithdrawAmount(allWithdrawAmount);
            }

            SysUserAccount sysUserAccount = sysUserService.getUserAccountByUserId(user.getUserId());
            //获取代付中的金额
            BigDecimal procceed = sysWithdrawService.selectSysProcceedWithdraw(user.getUserCode());
            if (procceed==null){
                procceed = BigDecimal.ZERO;
            }
            sysUserAccount.setBalance(sysUserAccount.getBalance().subtract(procceed));
            mmap.put("sysUserAccount", sysUserAccount);
        }
        mmap.put("sysIndexStatisticsRes", sysIndexStatisticsRes);

        mmap.put("version", Global.getVersion());
        return "main";
    }
}
